<?php

/**
 * question actions.
 *
 * @package    Nika
 * @subpackage question
 * @author     Alexey Shapran (alexey.shapran@gmail.com)
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class questionActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex(sfWebRequest $request)
	{

		if ($this->getUser()->isAuthenticated())
		{
			$this->questions = Question::showmyquestions($this->getUser()->getGuardUser()->getId());
			$this->form = new QuestionForm();
			$this->getUser()->setFlash('action','this');
		}
		else
		{
			$this->questions = Question::showallquestions();
			$this->getUser()->setFlash('action','all');
		}


		//adding pager
		$this->pager = new sfDoctrinePager('question', sfConfig::get('app_maxquestionperpage'));
		$this->pager->setQuery($this->questions);
		$this->pager->setPage($request->getParameter('page',1));
		$this->pager->init();


		$response = $this->getResponse();
		$response->setTitle(sfConfig::get('app_title')." | ".'Вопросы');
	}
	public function executeSubmit(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod('post'));
		$new_quest = new Question();
		$new_quest->question_text = strip_tags($this->getRequestParameter('question_text'));
		$new_quest->is_published = 1;
		$new_quest->user_id = $this->getUser()->getGuardUser()->getId();
		$new_quest->save();
		$fromwhere = $_SERVER['HTTP_REFERER'];
		$this->redirect($fromwhere);
	}
	public function executeShowall(sfWebRequest $request)
	{
		$this->setTemplate('index');

		$this->questions = Question::showallquestions();
		$this->form = new QuestionForm();
		$this->getUser()->setFlash('action','all');
			
		$this->pager = new sfDoctrinePager('question',sfConfig::get('app_maxquestionperpage'));
		$this->pager->setQuery($this->questions);
		$this->pager->setPage($request->getParameter('page',1));
		$this->pager->init();

		$response = $this->getResponse();
		$response->setTitle(sfConfig::get('app_title')." | ".'Вопросы');

	}
	public function executeDelete(sfWebRequest $request)
	{
		$quest_id = $request->getParameter('qid');
		$user_id = $this->getUser()->getGuardUser()->getId();
		$delete_query = Question::deletethis($quest_id, $user_id);
		$delete_query->execute();
		$this->redirect('question/index');
	}
}
